package com.examples.chartdirector.FinancialCharts;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JLabel;
import javax.swing.JPanel;

import com.examples.chartdirector.service.BiyingHttpServiceImplement;
import com.examples.chartdirector.service.IBiyingService;
import com.examples.chartdirector.ui.ChartPanel;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import ChartDirector.CandleStickLayer;
import ChartDirector.Chart;
import ChartDirector.ChartViewer;
import ChartDirector.FinanceChart;

public class FinanceChart2 extends ChartPanel {

	public static void createDemoPanel(JPanel chartContainer) {
		// Create a finance chart demo containing 100 days of data
//		int noOfDays = 100;
		int noOfDays = 65;

		// To compute moving averages starting from the first day, we need to get extra data points before
		// the first day
		int extraDays = 20;


		// Now we read the data from the table into arrays
		Date[] timeStamps = new Date[noOfDays + extraDays];
		double[] highData = new double[noOfDays + extraDays];
		double[] lowData = new double[noOfDays + extraDays];
		double[] openData = new double[noOfDays + extraDays];
		double[] closeData = new double[noOfDays + extraDays];
		double[] volData = new double[noOfDays + extraDays];

		try {
			final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			new BiyingHttpServiceImplement().historyQuery("002265", IBiyingService.Level.day, IBiyingService.Ex_rights.f,
					timeStamps.length, (json) -> {

						try {
							JsonElement arrayelement = JsonParser.parseString(json);
							JsonArray array = arrayelement.getAsJsonArray();
							for (int i = 0, index = 0; i < array.size(); i++, index++) {
								JsonElement element = array.get(i);
								JsonObject object = element.getAsJsonObject();
								String t = object.get("t").getAsString();
								double h = object.get("h").getAsDouble();
								double l = object.get("l").getAsDouble();
								double o = object.get("o").getAsDouble();
								double c = object.get("c").getAsDouble();
								double v = object.get("v").getAsDouble();
	
								timeStamps[index] = format.parse(t);
								highData[index] = h;
								lowData[index] = l;
								openData[index] = o;
								closeData[index] = c;
								volData[index] = v;
							}

							drawChart(timeStamps, highData, lowData, openData, closeData, volData, extraDays,
									chartContainer);
						} catch (Exception e) {

							StringWriter stringWriter = new StringWriter();
							PrintWriter writer=new PrintWriter(stringWriter);
							e.printStackTrace(writer);

							JLabel error=new JLabel(stringWriter.toString());
							chartContainer.add(error);
						}
					});

		} catch (Exception e) {
			StringWriter stringWriter = new StringWriter();
			PrintWriter writer=new PrintWriter(stringWriter);
			e.printStackTrace(writer);

			JLabel error=new JLabel(stringWriter.toString());
			chartContainer.add(error);
		}
	}

	private static void drawChart(Date[] timeStamps, double[] highData, double[] lowData, double[] openData,
			double[] closeData, double[] volData, int extraDays, JPanel chartContainer) {

		// Create a FinanceChart object of width 640 pixels
		FinanceChart c = new FinanceChart(640);
		c.setAxisOnRight(false);

		// Add a title to the chart
//		c.addTitle("Finance Chart Demonstration");
		c.addPlotAreaTitle(Chart.TopCenter, "<*font=mingliu.ttc,size=14*>\u5EFA\u8BBE\u5DE5\u4E1A");

		// Set the data into the finance chart object
		c.setData(timeStamps, highData, lowData, openData, closeData, volData, extraDays);

		// Add a slow stochastic chart (75 pixels high) with %K = 14 and %D = 3
//		c.addSlowStochastic(75, 14, 3, 0x006060, 0x606000);

		// Add the main chart with 240 pixels in height
		c.addMainChart(240).setMonthNames(
				new String[] { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月" });

		// Add a 10 period simple moving average to the main chart, using brown color
//		c.addSimpleMovingAvg(10, 0x663300);

		// Add a 20 period simple moving average to the main chart, using purple color
		c.addSimpleMovingAvg(20, 0x9900ff);

		// Add candlestick symbols to the main chart, using red/green for up/down days
		 CandleStickLayer cLayer = c.addCandleStick(0xd90000, 0x00A200);
		cLayer.getDataSet(0).setDataColor(0xd90000, Chart.SameAsMainColor);
		cLayer.getDataSet(1).setDataColor(0x00A200, Chart.SameAsMainColor);

		// Add 20 days donchian channel to the main chart, using light blue (9999ff) as the border and
		// semi-transparent blue (c06666ff) as the fill color
//		c.addDonchianChannel(20, 0x9999ff, 0xc06666ff);
		c.addBollingerBand(20, 2, 0x9999ff, 0xffffff);

		// Add a 75 pixels volume bars sub-chart to the bottom of the main chart, using red/green/grey for
		// up/down/flat days
//		c.addVolBars(75, 0xff9999, 0x99ff99, 0x808080);

		// Append a MACD(26, 12) indicator chart (75 pixels high) after the main chart, using 9 days for
		// computing divergence.
//		c.addMACD(75, 26, 12, 9, 0x0000ff, 0xff00ff, 0x008000);
		c.addMACD(75, 10, 5, 4, 0x0000ff, 0xff00ff, 0x008000).setDefaultFonts("simhei.ttf", "simhei.ttf");

		// Output the chart
		ChartViewer viewer = new ChartViewer();
		viewer.setChart(c);

		// Include tool tip for the chart
		viewer.setImageMap(cLayer.getHTMLImageMap(null));
		showInCenter(chartContainer,viewer);
	}

}
